﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;

namespace app.Api
{
    /*
        投资模块
    */
    public class ApiInvestment : ApiBase
    {
        public ApiInvestment(Controller ctx) : base(ctx) { }

        #region 自定义请求参数
        string req_capital_type { get { return GetStringParam(new ReqStringParamOption { Name = "capital_type" }); } }
        string req_area { get { return GetStringParam(new ReqStringParamOption { Name = "area" }); } }
        string req_investment_industry { get { return GetStringParam(new ReqStringParamOption { Name = "investment_industry" }); } }
        string req_investment_area { get { return GetStringParam(new ReqStringParamOption { Name = "investment_area" }); } }
        int req_amount_min { get { return GetIntParam(new ReqIntParamOption { Name = "amount_min" }); } }
        int req_amount_max { get { return GetIntParam(new ReqIntParamOption { Name = "amount_max" }); } }
        int req_term_min { get { return GetIntParam(new ReqIntParamOption { Name = "term_min" }); } }
        int req_term_max { get { return GetIntParam(new ReqIntParamOption { Name = "term_max" }); } }
        string req_investment_type { get { return GetStringParam(new ReqStringParamOption { Name = "investment_type" }); } }
        string req_needed_appendix { get { return GetStringParam(new ReqStringParamOption { Name = "needed_appendix" }); } }
        string req_requirement { get { return GetStringParam(new ReqStringParamOption { Name = "requirement" }); } }
        string req_thumbnail { get { return GetStringParam(new ReqStringParamOption { Name = "thumbnail" }); } }
        string req_appendix_ids { get { return GetStringParam(new ReqStringParamOption { Name = "appendix_ids" }); } }
        string req_investment_stage { get { return GetStringParam(new ReqStringParamOption { Name = "investment_stage" }); } }
        double req_min_return { get { return GetDoubleParam(new ReqDoubleParamOption { Name = "min_return" }); } }

        #endregion

        public void Do(string action)
        {
            switch (action)
            {
                case "page":
                    {
                        ResponseJson(GetPageResult<Investment>(string.Format("member_id = '{0}'", UserCode), G.DB.GetPrimaryKey<Investment>()));
                        break;
                    }
                case "get":
                    {
                        Investment investment = G.DB.GetById<Investment>(req_id);
                        ResponseJson(new ObjectResult { data = investment });
                        break;
                    }
                case "post":
                    {
                        Investment investment = new Investment { status = (int)INVESTMENT_STATUS.EXAMINING };
                        ApplyDataToInvestment(investment);
                        G.DB.Insert<Investment>(investment);
                        ResponseJson(new IDResult { id = investment.id });
                        break;
                    }
                case "draft":
                    {
                        Investment investment = new Investment { status = (int)INVESTMENT_STATUS.DRAFT };
                        ApplyDataToInvestment(investment);
                        G.DB.Insert<Investment>(investment);
                        ResponseJson(new IDResult { id = investment.id });
                        break;
                    }
                case "re_post":
                    {
                        Investment investment = G.DB.GetById<Investment>(req_id);
                        ApplyDataToInvestment(investment);
                        investment.status = (int)INVESTMENT_STATUS.EXAMINING;
                        G.DB.Update<Investment>(investment);
                        ResponseJson(new Result());
                        break;
                    }
                case "re_draft":
                    {
                        Investment investment = G.DB.GetById<Investment>(req_id);
                        ApplyDataToInvestment(investment);
                        investment.status = (int)INVESTMENT_STATUS.DRAFT;
                        G.DB.Update<Investment>(investment);
                        ResponseJson(new Result());
                        break;
                    }
                default: break;
            }
        }

        private Investment ApplyDataToInvestment(Investment obj)
        {
            obj.title = req_title;
            obj.member_id = UserCode;
            obj.capital_type = req_capital_type;
            obj.area = req_area;
            obj.investment_industry = req_investment_industry;
            obj.investment_area = req_investment_area;
            obj.amount_min = req_amount_min;
            obj.amount_max = req_amount_max;
            obj.term_min = req_term_min;
            obj.term_max = req_term_max;
            obj.investment_type = req_investment_type;
            obj.needed_appendix = req_needed_appendix;
            obj.requirement = req_requirement;
            obj.thumbnail = req_thumbnail;
            obj.appendix_ids = req_appendix_ids;
            obj.create_time = DateTimeEx.GetNowTimeStamp();
            return obj;
        }
    }

    enum INVESTMENT_STATUS
    {
        DRAFT = 0, // 草稿
        EXAMINING = 1, // 正在审核
        FAILED_EXAMINE = 2, // 审核失败
        POSTED = 3, // 已发布
        RECLAIMED = 4, // 已回收
        DELETED = 5 // 已删除
    }
}